package com.sumainfor.pc.epc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.HtmlUtil;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.*;
import com.sumainfor.pc.epc.feign.GetUserInfoFeign;
import com.sumainfor.pc.epc.feign.Housingauditfeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.epc.service.DecorateplanService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class DecorateplanServiceImpl implements DecorateplanService {

    private ReentrantLock lock = new ReentrantLock(false);//

    Logger log = LoggerFactory.getLogger(DecorateplanServiceImpl.class);// 日志


    @Autowired
    Housingauditfeign housingConInfoFeign;
    @Autowired
    private DecorateplanMapper mapper;
    @Autowired
    DecoratedBeingMapper BeingMapper;
    @Autowired
    private DecorateMapper decorateMapper;
    @Autowired
    Tcepc_housing_plan planmapper;
    @Autowired
    GetUserInfoFeign getUserInfoFeign;
    @Autowired
    Housingauditfeign hfeign;
    @Autowired
    UtilsMapper utilsMapper;

    @Autowired
    ZzuulFeign ZzuulFeign;

    @Override
    public JsonResult deleDecoratePlanInfo(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            params.put("FinState", 1);
            JsonResult jsonResult = hfeign.updateHousingByFinState(params);//首先修改房源的状态 已审核待参与的状态需要

            if (jsonResult.isSuccess()==true) {

                if (mapper.deleDecoratePlanInfo(params) > 0) {
                    mapper.deleDecoratePlanInfoDeploy(params);//删除配置
                    String HouConId = planmapper.getHouConId(params);////通过房源id获取HouConId
                    params.put("PalHouId", HouConId);
                    mapper.deleDetcepc_housing_plan_info(params);//删除无限网络和有限电视
                    result.putSuccess("操作成功!");
                } else {
                    result.putFailed("操作失败！");
                }

            } else {
                result.putFailed("操作失败！");
            }


        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }

        return result;
    }

    @Override
    public JsonResult updatePlanInfobySupId(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        try {

            String SysUserId = ContextUtils.getUserId();
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            if (mapper.updatePlanInfobySupId(params) > 0) {

                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }

        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        }

        return result;
    }

    /**
     * 根据装修计划id 查询出详情
     *
     * @param params
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public JsonResult getDecorationPlanForDetails(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {
            Map<String, Object> map = mapper.getDecorationPlanForDetails(params);
            map.put("couHou", decorateMapper.getcountPlansId(params.get("PlanId").toString()));//根据房源id统计出详表房源套数
            List<Map<String, Object>> mapList = mapper.getDecorationInfo(params);//查询详表的房源id

            Map<String, Object> Houmap = new HashMap<String, Object>();
            Houmap.put("HouId", mapList.get(0).get("HouId"));
            String HouConId = planmapper.getHouConId(Houmap);////通过房源id获取HouConId
            Map<String, Object> maps = planmapper.getConInfo1(HouConId, 3);//首先查询出这个无限网络
            Map<String, Object> map1 = new HashMap<String, Object>();
            JSONObject jsonObject = JSONObject.parseObject(maps.get("WiFi").toString());//JSON转成map
            map1.put("IfWiFi", maps.get("IfWiFi"));//获取是否开启
            map1.put("yys", jsonObject.get("yys"));//获取网络运行商
            map1.put("lxr", jsonObject.get("lxr"));//获取联系人
            map1.put("lxhm", jsonObject.get("lxhm"));//获得联系号码
            Map<String, Object> mapz = planmapper.getConInfo1(HouConId, 4);//首先查询出有线电视
            map1.put("IfYx", mapz.get("IfWiFi"));
            map.put("wifilist", map1);
            //查询房源
            JsonResult reslist = housingConInfoFeign.gettcmktHousing(mapList);
            String Code = reslist.get("errorCode").toString();
            List<Map<String, Object>> Housinglist = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> SupHousing = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> suplist = mapper.getSupplier(params);
            if ("0".equals(Code)) {
                List<Map<String, Object>> reslistmap = (List<Map<String, Object>>) reslist.get("result");//获取房源集合
                List<Map<String, Object>> reslistmap1 = (List<Map<String, Object>>) reslist.get("result");
                for (Map<String, Object> objectMap : reslistmap) {
                    Map<String, Object> omap = new HashMap<String, Object>();
                    Map<String, Object> infomap = mapper.getJoInDecorationInfo(objectMap.get("HouId").toString());
                    objectMap.put("PlansId", infomap.get("PlansId"));
                    objectMap.put("Contact", infomap.get("Contact"));
                    objectMap.put("SupType", infomap.get("SupType"));
                    objectMap.put("SupPrice", infomap.get("SupPrice"));
                    omap.putAll(objectMap);
                    Housinglist.add(omap);
                }
                for (Map<String, Object> stringObjectMap : suplist) {
                    List<Map<String, Object>> supHousinglist = new ArrayList<Map<String, Object>>();
                    for (Map<String, Object> objectMap : reslistmap1) {
                        Map<String, Object> infomap = mapper.getJoInDecorationInfo1(objectMap.get("HouId").toString(), stringObjectMap.get("SupId").toString());
                        if (infomap != null) {
                            Map<String, Object> omap = new HashMap<String, Object>();
                            objectMap.put("PlansId", infomap.get("PlansId"));
                            objectMap.put("Contact", infomap.get("Contact"));
                            objectMap.put("SupType", infomap.get("SupType"));
                            objectMap.put("SupPrice", infomap.get("SupPrice"));
                            omap.putAll(objectMap);
                            supHousinglist.add(omap);
                        }
                    }
                    stringObjectMap.put("supHousinglist", supHousinglist);
                    Integer sumSupPrice = mapper.getSumSupPrice(stringObjectMap.get("SupId").toString(), map.get("PlanId").toString());
                    if (null != sumSupPrice) {
                        stringObjectMap.put("sumSupPrice", sumSupPrice);
                    } else {
                        stringObjectMap.put("sumSupPrice", 0);
                    }
                    SupHousing.add(stringObjectMap);
                }
            }
            map.put("housingPlanInfo", mapper.getSumhousingPlanInfo(params));//询配置清单
            map.put("Housinglist", Housinglist);//装修房源
            map.put("SupHousinglist", SupHousing);
            result.put(map);
        } catch (Exception e) {

            log.info("e======>" + e);
            result.putFailed("服务器繁忙。。。");
        }
        return result;
    }

    @Override
    public JsonResult getPlan_deploy(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        String userid = mapper.getSysUserId(params);//获取装修计划负责人id
        List<String> sliat = new ArrayList<String>();
        sliat.add(userid);
        JsonResult<List<Map<String, Object>>> res = getUserInfoFeign.getUserInfo(sliat);
        List<Map<String, Object>> data = res.getResult();
        String username = MapUtils.getString(data.get(0), "username");
        log.info("res======>" + res);
        String userid1 = mapper.getgjuserid(params);//获取验房管家id
        String yfusername;
        String mobile;
        if(userid1==null||"".equals(userid1)){

            yfusername ="待分配";
            mobile = "待分配";

        }else {
            List<String> getgjuserid = new ArrayList<String>();
            getgjuserid.add(userid1);
            JsonResult<List<Map<String, Object>>> res1 = getUserInfoFeign.getUserInfo(getgjuserid);
            List<Map<String, Object>> data1 = res1.getResult();
             yfusername = MapUtils.getString(data1.get(0), "username");//验房名字
            mobile = MapUtils.getString(data1.get(0), "mobile");//验房名字
        }


        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = mapper.getPlan_deploy(params);
        map.put("Dep", list);//获得8大属性
        map.put("username", username);//工程负责人
        map.put("yfusername", yfusername);//工程负责人
        map.put("mobile", mobile);//工程负责人
        map.put("getdatadeom", mapper.getdatadeom(params));//获得装修时间
        result.put(map);//查询
        return result;
    }

    /**
     * 根据商品名称查询归属的房源
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult getHouId(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            Map<String, Object> houmap = new HashMap<String, Object>();
            List<Map<String, Object>> houlist = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> datalist = mapper.getHouId(params);
            for (Map<String, Object> map : datalist) {
                Map<String, Object> ma = new HashMap<String, Object>();
                ma.put("HouId", map.get("HouId"));
                ma.put("ComNumber", map.get("ComNumber"));
                houlist.add(ma);
            }
            houmap.put("Hou", houlist);
            //查询房源
            JsonResult reslist = housingConInfoFeign.getHougingbyhid(houmap);
            String Code = reslist.get("errorCode").toString();
            if ("0".equals(Code)) {
                List<Map<String, Object>> datamaplist = (List<Map<String, Object>>) reslist.get("result");
                result.put(datamaplist);
            } else {
                result.put(new ArrayList<Map<String, Object>>());
            }

        } catch (Exception e) {
            log.info("E=======>" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult getcommodityBytype(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {
            result.put(BeingMapper.getcommodityBytype(params));
        } catch (Exception e) {
            log.info("E=======>" + e);
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    /**
     * 单个房源查询 装修进度 以及网络
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult gettcepchousingplaninfo(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {
            Map<String, Object> resmap = new HashMap<String, Object>();
            params.put("SupType", 3);//
            Map<String, Object> maps = BeingMapper.getWiFi(params); //首先查询出这个无限网络
            log.info("-------------------------------");
            log.info("-------------------------------");
            log.info("-------------------------------");
            Map<String, Object> wifimap = new HashMap<String, Object>();
            JSONObject jsonObject = JSONObject.parseObject(maps.get("WiFi").toString());//JSON转成map
            wifimap.put("IfWiFi", maps.get("IfWiFi"));//获取是否开启
            wifimap.put("IfWF", maps.get("IfWiFi"));//获取是否开启
            wifimap.put("yys", jsonObject.get("yys"));//获取网络运行商
            wifimap.put("lxr", jsonObject.get("lxr"));//获取联系人
            wifimap.put("lxhm", jsonObject.get("lxhm"));//获得联系号码
            wifimap.put("InspectState", maps.get("InspectState"));//状态
            wifimap.put("InspectNotes", maps.get("InspectNotes"));//备注
            wifimap.put("InspectTime", maps.get("InspectTime"));//审核时间
            params.put("SupType", 4);//
            Map<String, Object> mapz = BeingMapper.getWiFi(params);//首先查询出有线电视
            wifimap.put("IfYx", mapz.get("IfWiFi"));
            wifimap.put("YxInspectState", mapz.get("InspectState"));//状态
            wifimap.put("YxInspectNotes", mapz.get("InspectNotes"));//备注
            wifimap.put("YxInspectTime", mapz.get("InspectTime"));//审核时间
            resmap.put("wifilist", wifimap);//无限网络
            resmap.put("info", BeingMapper.gettcepc_decorate_plan_info(params));//装修状态
            result.put(resmap);
        } catch (Exception e) {

            log.info("E---->" + e);
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    @Override
    public JsonResult gethousing_plan_infoSupId(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        try {

            result.put(BeingMapper.gethousing_plan_infoSupId(params));
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        }
        return result;
    }

    @Override
    public JsonResult deleterests(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {

            if (BeingMapper.deleterests(params) > 0) {

                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }

        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    /**
     * 新增一个其他费用
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult saverests(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            String UUID = null;
            Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                put("uuid", "ResId");
                put("database", Constants.DATA_SOURCE_TCEPC);
                put("surface", "tcepc_decorate_rests");
            }});
            if (uuid != null) {
                int number = Integer.valueOf(uuid.get("uuid").toString());
                UUID = ComUtils.getLocalTrmSeqNum("Res", number);
            } else {
                UUID = ComUtils.getLocalTrmSeqNum("Res", 0);
            }
            params.put("ResId", UUID);
            String SysUserId = ContextUtils.getUserId();
            params.put("SysUserId", SysUserId);//装修计划负责人id
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);

            if (BeingMapper.saverests(params) > 0) {

                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }

        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    /**
     * 新增一个其他费用
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult savesrests(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            List<Map<String, Object>> datalist = (List<Map<String, Object>>) params.get("datalist");//

            int i = 0;
            for (Map<String, Object> objectMap : datalist) {
                String UUID = null;
                Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                    put("uuid", "ResId");
                    put("database", Constants.DATA_SOURCE_TCEPC);
                    put("surface", "tcepc_decorate_rests");
                }});
                if (uuid != null) {
                    int number = Integer.valueOf(uuid.get("uuid").toString());
                    UUID = ComUtils.getLocalTrmSeqNum("Res", number);
                } else {
                    UUID = ComUtils.getLocalTrmSeqNum("Res", 0);
                }
                objectMap.put("ResId", UUID);
                String SysUserId = ContextUtils.getUserId();
                objectMap.put("SysUserId", SysUserId);//装修计划负责人id
                objectMap.put("by", SysUserId);//操作人
                objectMap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                objectMap.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(objectMap);
                i = BeingMapper.saverests(objectMap);

            }
            if (i > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    /**
     * 移动数据
     *
     * @param params
     */
    @Override
    public void movement(Map<String, Object> params) {
        Map<String, Object> reqMap = new HashMap<String, Object>();
        //首先先查询出来
        //先查询出 配置主表
        reqMap.put("HouingCon", BeingMapper.getHouingCon(params));
        //查询出配置详表
        reqMap.put("HouingConInfo", BeingMapper.getHouingConInfo(params));

        /**
         * 首先查询房源的房间号id
         */
        List<Map<String, Object>> hId = BeingMapper.getHouingHouConId(params);
        /**
         * 查询出无线网络 电视
         */
        List<Map<String, Object>> configList = BeingMapper.getHouingConInfoWiFi(params);

        List<Map<String, Object>> data = Lists.newArrayList();

        for (int i = 0; i < hId.size(); i++) {
            Map<String, Object> idItem = hId.get(i);
            for (int j = 0; j < configList.size(); j++) {
                Map<String, Object> conItem = configList.get(j);
                Map<String, Object> item = Maps.newHashMap(conItem);
                item.putAll(idItem);
                data.add(item);
            }
        }
        log.info("Result Data.size => {}", data.size());
        log.info("Result Data => {}", data);
        //无线网络电视
        reqMap.put("HouingConInfoWiFi", data);
        hfeign.saveHousingCon(reqMap);//远程调用
    }

    @Override
    public JsonResult updaterests(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            String SysUserId = ContextUtils.getUserId();
            params.put("SysUserId", SysUserId);//装修计划负责人id
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            if (BeingMapper.updaterests(params) > 0) {

                if(!"".equals(params.get("IfStatus").toString())){//如果IfStatus不为空那就是经理操作的

                    log.info("经理操作");

                    if(BeingMapper.getCourests(params)==0){
                        if (BeingMapper.countInspectState(params) == 0 && BeingMapper.countIfCheckState(params) == 0
                                && BeingMapper.plan_building(params) == 0 && BeingMapper.isCourests(params)==0) {

                            params.put("IfCheckState", 0);
                            params.put("CheckStatus", 3);
                            BeingMapper.updateIfCheckStatea(params);
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                            params.put("CheckRealityTime", df.format(new Date()));
                            params.put("CheckState", 2);
                            if (BeingMapper.updateCheckState(params) > 0) {
                                params.put("State", 7);
                                hfeign.updatehousingByHousId(params);//修改房源状态
                                movement(params);//如果这个房源验房全部通过了之后 把工程部里面的配置数据全部移到市场部的房源配置表里面去
                                if (BeingMapper.getCheckStateinfo(params) == 0) {
                                    BeingMapper.updatedecorate_plan(params);//当着一批的所有房源全部完成之后自动的改变这个批次的状态
                                }
                                //result.putSuccess("操作成功");
                            } else {
                                //  result.putFailed("操作失败");
                            }
                        }
                    }
                }else {
                    log.info("助理操作");
                }
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    ///////////
    @Override
    public JsonResult updateCivilState(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            // new Date()为获取当前系统时间
            params.put("CivilRealityTime", df.format(new Date()));
            params.put("CivilState", 2);
            String SysUserId = ContextUtils.getUserId();
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            if (BeingMapper.updateCivilState(params) > 0) {
                params.put("State", 4);
                housingConInfoFeign.updatehousingByHousId(params);//修改房源状态
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult updateCheckState(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {

            if (BeingMapper.countInspectState(params) == 0 && BeingMapper.countIfCheckState(params) == 0) {

                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                // new Date()为获取当前系统时间
                params.put("CheckRealityTime", df.format(new Date()));
                params.put("CheckState", 2);
                String SysUserId = ContextUtils.getUserId();
                params.put("by", SysUserId);//操作人
                params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(params);
                if (BeingMapper.updateCheckState(params) > 0) {
                    params.put("State", 7);
                    housingConInfoFeign.updatehousingByHousId(params);//修改房源状态
                    if(BeingMapper.getCheckStateinfo(params)==0){
                        BeingMapper.updatedecorate_plan(params);//当着一批的所有房源全部完成之后自动的改变这个批次的状态
                    }
                    result.putSuccess("操作成功");
                } else {
                    result.putFailed("操作失败");
                }
            } else {
                result.putSuccess("房源配置验收中不能完成验房阶段！");
            }


        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult updateFitmentState(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {

            Map<String, Object> datalist = BeingMapper.getState(params);

            if ("2".equals(datalist.get("CivilState").toString())) {

                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                // new Date()为获取当前系统时间
                params.put("FitmentRealityTime", df.format(new Date()));
                params.put("FitmentState", 2);

                String SysUserId = ContextUtils.getUserId();
                params.put("by", SysUserId);//操作人
                params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(params);
                if (BeingMapper.updateFitmentState(params) > 0) {
                    BeingMapper.updatedeploy(params);
                    params.put("State", 6);
                    housingConInfoFeign.updatehousingByHousId(params);//修改房源状态
                    result.putSuccess("操作成功");
                } else {
                    result.putFailed("操作失败");
                }


            } else {
                result.putSuccess("请先完成土建阶段！");
            }


        } catch (Exception e) {
            log.info("e--->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult updatedeploybyDepId(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {

            Map<String, Object> datalist = BeingMapper.getState(params);

            if ("2".equals(datalist.get("CivilState").toString())) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                // new Date()为获取当前系统时间
                params.put("DepTime", df.format(new Date()));
                params.put("DepType", 2);

                String SysUserId = ContextUtils.getUserId();
                params.put("by", SysUserId);//操作人
                params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(params);

                if (BeingMapper.updatedeploybyDepId(params) > 0) {

                    if (BeingMapper.getCounttype(params) == 0) {
                        params.put("FitmentRealityTime", df.format(new Date()));
                        params.put("FitmentState", 2);
                        BeingMapper.updateFitmentState(params);
                        params.put("State", 6);
                        housingConInfoFeign.updatehousingByHousId(params);//修改房源状态
                    }

                    result.putSuccess("操作成功");
                } else {
                    result.putFailed("操作失败");
                }
            } else {

                result.putSuccess("请先完成土建阶段！");
            }

        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }


    @Override
    public JsonResult updateCheckNoticeTime(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            String userid1 = mapper.getgjuserid(params);//获取验房管家id

            if(mapper.getCouhousing_plan(params)==0){

//                if(userid1==null||"".equals(userid1)){
//                    result.putSuccess("该房源未分配管家不能提交验房！");
//                }else{
                    String SysUserId = ContextUtils.getUserId();
                    params.put("CheckStatus", 1);//管家验房阶段状态 0.无状态   1.待验房（通知之后）  2.验房未通过  3.验房通过
                    params.put("by", SysUserId);//操作人
                    params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonUpdateParms(params);

                    //添加管家验房编号
                    BeingMapper.UpdateHousingPlanBuilding(params);

                    if (BeingMapper.updateCheckNoticeTime(params) > 0) {
                        BeingMapper.updateInspectState(params);

                        //新的特价房源审核通知
                        CustomMap data = CustomMap.create("MeType", 10)
                                .put("Title", "房源待验收")
                                .put("Content", HtmlUtil.stringToHTML("注意！有新的房源待验收 请及时验收！；", "", Color.blue))
                                .put("by", ContextUtils.getUserId())
                                .put("term", 2);
                        ZzuulFeign.AddMessage(data);

                        result.putSuccess("操作成功");
                    } else {
                        result.putFailed("操作失败");
                    }
//                }

            }else {
                result.putSuccess("该房源未填写面积和朝向不能提交验房！");
            }


        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult updateIfCheckState(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            params.put("CheckStatus",1);
            if (BeingMapper.updateIfCheckState(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult updateIdentifier(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            String SysUserId = ContextUtils.getUserId();
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            if (BeingMapper.updateIdentifier(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult upodateAerTowpojo(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {


            String SysUserId = ContextUtils.getUserId();
            params.put("by", SysUserId);//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);

            if (BeingMapper.upodateAerTowpojo(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult housing_plan_info(Map<String, Object> params) {
        JsonResult result = new JsonResult();

        /**
         * 加锁操作
         */
        lock.lock();
        try {
            Map<String, Object> PlanInfo = (Map<String, Object>) params.get("decorateplaninfo");//获取详表对象
            List<Map<String, Object>> housinginfo = (List<Map<String, Object>>) params.get("housinginfo");//获取无限网络集合
            Map<String, Object> houingpojo = (Map<String, Object>) params.get("housingpojo");//获取预销对象;
            int i= (int) houingpojo.get("HouState");
            log.info("i>>>>>>"+i);
            if(i==0){//
                houingpojo.put("FutTime","");
            }
            JsonResult result1= hfeign.updatehousingtt(houingpojo);
            log.info("result1:"+result1);
            if ("0".equals(result1.get("errorCode").toString())){

            log.info("PlanInfo:"+PlanInfo);
                String SysUserId = ContextUtils.getUserId();
                PlanInfo.put("by", SysUserId);//操作人
                PlanInfo.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                PlanInfo.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(PlanInfo);
                if (BeingMapper.updatetinfo(PlanInfo) > 0) {

                    for (Map<String, Object> objectMap : housinginfo) {

                        log.info("objectMap:"+objectMap);

                        objectMap.put("by", SysUserId);//操作人
                        objectMap.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                        objectMap.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                        ParamsCommon.commonUpdateParms(objectMap);
                        if (BeingMapper.housing_plan_info(objectMap) > 0) {
                            result.putSuccess("操作成功");
                        } else {
                            result.putFailed("操作失败");

                        }
                    }
                }

            }else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            log.info("E==="+e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }

    @Override
    public JsonResult updateIfUpload(Map<String, Object> params) {
        JsonResult result  = new JsonResult();

        if(mapper.updateIfUpload(params)>0){

            result.putSuccess("操作成功");
        }else {
            result.putFailed("操作失败");
        }
        return result;
    }
    /**
     *编辑装修计划修改智能设备
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult updateHpiInte(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            if (params.get("Intelligent")==null || params.get("Identifier")==null || params.get("PalHouId")==null
            || params.get("HouConIds")==null){
                return result.putFailed("参数不正确");
            }
            params.put("by", ContextUtils.getUserId());//操作人
            params.put("term", "3");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            params.put("fnc", this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonUpdateParms(params);
            Integer count=BeingMapper.updateHpiInte(params);
            if (count> 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("服务器繁忙");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception e) {
            result.putFailed("服务器繁忙");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            lock.unlock();//解锁
        }
        return result;
    }
}
